package com.yc.bank.web;

import com.google.gson.Gson;
import com.yc.bank.biz.OprecordBiz;
import com.yc.bank.entity.Oprecord;
import com.yc.bank.entity.Result;
import com.yc.bank.service.IAccountService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet(name = "QueryServlet", value = "/query.s")
public class QueryServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        IAccountService accountService = ctx.getBean(IAccountService.class);
        String name = (String) request.getSession().getAttribute("name");

        OprecordBiz oprecordBiz = new OprecordBiz();

        List<Oprecord> query = oprecordBiz.query(name);
        DateTimeFormatter time =  DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        List<String> date = new ArrayList<>();
        for(Oprecord o : query) {
            date.add(o.getOptime().format(time));
        }
        List<Object> list = new ArrayList<>();
        list.add(query);
        list.add(date);
        System.out.println(list);
        Result result = new Result(1,"查询成功",list);
        String json = new Gson().toJson(result);

        response.getWriter().append(json);
    }
}